const {contextBridge, ipcRenderer} = require('electron');
const path = require("path");


const mainOperation = (row) => {
  ipcRenderer.send('operation-photo', row);
};

const mainWindow = (data) => {
  ipcRenderer.send('window-event', data);
};

const sendWebSocktMessage = (data) => {
  ipcRenderer.send('send-message-to-websocket', data);
};


contextBridge.exposeInMainWorld('myApi', {
   getImageBase64: (filePath) => ipcRenderer.invoke('get-image-base64', filePath)
   .then(res => {
      //console.log('Preload get-image-base64收到:', res); // 检查这里是否有输出
      return res;
    }),
  merchantAuth: (data) => ipcRenderer.invoke('merchant-auth', data)
    .then(res => {
      console.log('Preload merchant-auth收到:', res); // 检查这里是否有输出
      return res;
    }),
    createCustomer: (data) => ipcRenderer.invoke('create-customer', data)
    .then(res => {
      console.log('Preload create-customer收到:', res); // 检查这里是否有输出
      return res;
    }),
    getCustomerList: (data) => ipcRenderer.invoke('get-customer-list', data)
    .then(res => {
      console.log('Preload get-customer-list收到:', res); // 检查这里是否有输出
      return res;
    }),
    imageLogList: (data) => ipcRenderer.invoke('image-log-list', data)
    .then(res => {
      console.log('Preload get-customer-list收到:', res); // 检查这里是否有输出
      return res;
    }),
  mainOperation,
  mainWindow,
  sendWebSocktMessage,
  // receiveMessageMerchantAuth,
  receiveMessageFromMainProcess: (callback) => {
    ipcRenderer.on('login-data', (event, data) => {
      console.log(data)
      callback(data);
    });
  },
  receiveMessageFromMainStutas: (callback) => {
    ipcRenderer.on('flash-data', (event, data) => {
      console.log(data)
      callback(data);
    });
  },
  receiveWebSocktMessage: (callback) => {
    ipcRenderer.on('websocket-message', (event, data) => {
      //console.log(data)
      callback(data);
    });
  },
  receiveMessageAppUpdate: (callback) => {
    ipcRenderer.on('app-update', (event, data) => {
      //console.log("app-update")
      callback(data);
    });
  },
  receiveMessageDownLoading: (callback) => {
    ipcRenderer.on('down-loading', (event, data) => {
      //console.log("app-update")
      callback(data);
    });
  }
});

// const {execFile} = require('child_process');
// // 要执行的 .exe 文件路径
// const exePath = path.resolve(__dirname, 'python', 'ocr_flask.py');
// //const exePath = './preload/python/getCaremInfo.exe';
// // 传递给 .exe 文件的参数（可选）
// const args = ['arg1', 'arg2'];
//
// execFile(exePath, args, (error, stdout, stderr) => {
//   if (stderr) {
//     console.error(`执行文件时发生stderr错误: ${stderr}`);
//     return;
//   }
//   if (error) {
//     console.error(`执行文件时发生error错误: ${error}`);
//     return;
//   }
//   if (stdout) {
//     console.log(`.exe 文件的输出：${stdout}`);
//   }
// });

// const pythonScriptPath = path.join(__dirname, 'python\\ocr_flask.py');
// // 启动 Flask 服务
// execFile(`python "${pythonScriptPath}"`, (error, stdout, stderr) => {
//   if (error) {
//     console.error(`exec error: ${error}`);
//     return;
//   }
//   console.log(`stdout: ${stdout}`);
//   console.error(`stderr: ${stderr}`);
// });

const { spawn } = require('child_process');

const pythonProcess = spawn('python', ['D:\\project\\electronDataCollection\\preload\\python\\ocr_flask.py']);

pythonProcess.on('error', (err) => {
  console.error('启动 Python 失败:', err);
});

pythonProcess.stdout.on('data', (data) => {
  console.log(`Python : ${data}`);
});

pythonProcess.stderr.on('data', (data) => {
  console.log(`Python : ${data}`);
});


